package com.zbkj.crmeb.supplier.controller;


import cn.hutool.core.util.StrUtil;
import com.common.CommonPage;
import com.crmeb.core.common.CommonResult;
import com.zbkj.crmeb.merchant.admin.controller.AdminBaseController;
import com.zbkj.crmeb.store.service.SupplierService;
import com.zbkj.crmeb.supplier.model.SupplierAddress;
import com.zbkj.crmeb.supplier.request.SupplierAddressRequest;
import com.zbkj.crmeb.supplier.request.SupplierSaveAddressRequest;
import com.zbkj.crmeb.supplier.service.SupplierAddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author cdm
 * @since 2023-08-28
 */
@RestController
@RequestMapping("api/admin/chant/supplier-address")
@Api(tags = "供应商售后地址")
public class SupplierAddressController extends AdminBaseController {

    @Resource
    private SupplierAddressService supplierAddressService;
    @Resource
    private SupplierService supplierService;

    @ApiOperation(value = "地址列表")
    @GetMapping(value = "/list")
    public CommonResult<CommonPage<SupplierAddress>> list(SupplierAddressRequest request) {
        Map<Integer, String> supplierNameMap = new HashMap<>();
        List<SupplierAddress> list = supplierAddressService.addressList(request);
        list.parallelStream().forEach(d -> {
            String name = supplierNameMap.get(d.getSupplierId());
            if (StrUtil.isEmpty(name)) {
                name = supplierService.getSuppliersName(d.getSupplierId().toString());
                supplierNameMap.put(d.getSupplierId(), name);
            }
            d.setSupplierName(name);
        });
        return CommonResult.success(CommonPage.restPage(list));
    }

    @ApiOperation(value = "新增/编辑售后地址")
    @PostMapping(value = "/save")
    public CommonResult<String> saveOrUpdateAddress(@RequestBody @Validated SupplierSaveAddressRequest request) {
        return supplierAddressService.saveOrUpdateAddress(request);
    }

    @ApiOperation(value = "删除售后地址")
    @DeleteMapping(value = "/delete/{id}")
    public CommonResult<String> deleteAddress(@ApiParam(value = "地址id", required = true) @PathVariable("id") Integer id) {
        return supplierAddressService.deleteTakeAddress(id);
    }

}

